package user

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/userServiceClient/userservice"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"

	agentClient "gitee.com/liuxuezhan/ar-platform/rpc-client/agentClient/client"
)

type NextLevelListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 会员管理 - 下级会员列表/查询
func NewNextLevelListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *NextLevelListLogic {
	return &NextLevelListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *NextLevelListLogic) buildRpcReq(req *types.UserChildSearchRequest) *userservice.UserChildSearchRequest {
	rpcReq := &userservice.UserChildSearchRequest{
		Page:             req.Page,
		PageSize:         req.PageSize,
		Id:               req.AgentId,
		ChildId:          req.ChildId,
		Nickname:         req.Nickname,
		MinLevel:         req.MinLevel,
		MaxLevel:         req.MaxLevel,
		RegisterDevice:   req.RegisterDevice,
		Status:           req.Status,
		Order:            req.OrderType,
		RegisterStart:    req.RegisterStart,
		RegisterEnd:      req.RegisterEnd,
		TransactionStart: req.TransactionStart,
		TransactionEnd:   req.TransactionEnd,
		VipLevel:         req.VipLevel,
	}
	return rpcReq
}

func (l *NextLevelListLogic) NextLevelList(req *types.UserChildSearchRequest) (resp *types.UserChildSearchResponse, err error) {
	rpcReq := l.buildRpcReq(req)
	searchResp, err := l.svcCtx.UserServiceRPC.UserChildSearch(l.ctx, rpcReq)
	if err != nil {
		logc.Error(l.ctx, "UserChildSearch error: %v", err)
		return &types.UserChildSearchResponse{
			Pagination: types.Pagination{},
			List:       []*types.UserChild{},
		}, nil
	}

	// agent info
	agentResp, err := l.svcCtx.AgentRPC.GetAgentInfoByUserIds(l.ctx, &agentClient.UserIdsRequest{UserIds: []int64{req.AgentId}})
	if err != nil {
		logc.Error(l.ctx, "Agent GetAgentInfoByUserIds rpc call error: %v", err)
		return &types.UserChildSearchResponse{
			Pagination: types.Pagination{},
			List:       []*types.UserChild{},
		}, nil
	}
	agentLevel := int64(0)
	if agent, ok := agentResp.Data[req.AgentId]; ok {
		agentLevel = agent.Level
	}

	userList := make([]*types.UserChild, len(searchResp.UserList))
	for index, item := range searchResp.UserList {
		userList[index] = &types.UserChild{
			Id:             item.Id,
			Account:        item.Account,
			VipLevel:       item.VipLevel,
			Status:         item.Status,
			IsOnline:       item.IsOnline,
			RegisterDevice: item.RegisterDevice,
			IsBlacklist:    item.IsBlacklist,
			Remark:         item.Remark,
			ParentId:       item.ParentId,
			Level:          item.Level - int32(agentLevel),
			ChildNum:       item.ChildNum,
			AllChildNum:    item.AllChildNum,
			RegistAt:       item.RegistAt,
			BetAmount:      item.BetAmount,
			BetCount:       item.BetCount,
			RechargeAmount: item.RechargeAmount,
			RechargeCount:  item.RechargeCount,
			WithdrawAmount: item.WithdrawAmount,
			WithdrawCount:  item.WithdrawCount,
			RebateAmount:   item.RebateAmount,
			RebateCount:    item.RebateCount,
			Balance:        item.Balance,
		}
	}
	resp = &types.UserChildSearchResponse{
		Pagination: types.Pagination{
			Page:      searchResp.Pagination.Page,
			PageSize:  searchResp.Pagination.PageSize,
			Total:     searchResp.Pagination.Total,
			TotalPage: searchResp.Pagination.TotalPage,
		},
		List: userList,
	}
	return
}
